#include "ResegUP.h"

ResegUP::ResegUP(void)
{
	Resolution = 1; 
	DigitalMax	=	4096;
	DigitalMin	=	0;
	DigitalOffset = 0; 
	
	Initialize(4,128,5);
	
	
	MyName = _("ResegUP Monitor");
	//Da cambiare,  fare una piccola GUI per nome ed ID_Runner

	Labels[0] = wxT("ECG");
	Labels[1] = wxT("AccX");
	Labels[2] = wxT("AccY");
	Labels[3] = wxT("AccZ");

	ResDialog = NULL;
} 

ResegUP::~ResegUP(void)
{
}

void *ResegUP::Entry(void)
{
	SQLBIGINT LastTime;
	SQLBIGINT Time;

	SampleStruct	Signal;
	memset(&Signal,0,sizeof(Signal));
	Signal.Data.DataLen	=	Channels;
	memset(&Signal,0,sizeof(Signal));

	GetUltimoTempo(&LastTime);
	Esci = false;
	while(!Esci)
	{
		if (IsRunning)
		{
			db->CommitTrans();
			if (ID_Runner_Old != ID_Runner)
			{
				ID_Runner_Old = ID_Runner;
				this->pPlotFrame->CleanPlot();
				GetUltimoTempo(&LastTime); //Azzero tempo
			}
			GetUltimoTempo(&Time);
			if (Time>LastTime)
			{
				GetPutECGeACCfromDB(&Signal,&LastTime,Time);
			}
			else
			{
				Sleep(500);
			}
		}
		else
		{
			Sleep(100);
		}
	}

	return NULL;
}

void ResegUP::StartDevice(wxString FileName,wxString FilePath,bool File)
{
	int ID_List[255];	//ID_Runner presenti nel DB
	int ID_Len;			//Quanti sono gli ID_Runner differenti

	Instrument::StartDevice(FileName,FilePath,File);
	IsRunning=true;
	ID_Len = ReadID(ID_List);
	ResDialog = new Resegup_Dialog(NULL, &ID_Runner);
	ResDialog->SetIDList(ID_List,ID_Len);
	ResDialog->Show();
}

void ResegUP::StopDevice(void)
{
	if (ResDialog!=NULL)
	{
		if (ResDialog->IsShown())
			ResDialog->Hide();
		
		ResDialog->Destroy();
	}
	Instrument::StopDevice();
	IsRunning = false;
	Sleep (500);
	Esci = true;
	
}

int ResegUP::OpenPort(const char *OpenVal1, const char *OpenVal2, const char *OpenVal3)
{
	//Inserire apertura DB e lettura primo valore Timestamp
	//HANDLE CREATION
	wxDbConnectInf DbConnectInf(NULL,wxString(OpenVal1,wxConvUTF8),wxT(""),wxT(""));
	if (!DbConnectInf.GetHenv())
	{
		wxMessageBox(wxT("Error: Cannot Allocate ODBC env handle"));
		return -1;
	}
	else
	{
		//DATABASE CONNECTION
		db = wxDbGetConnection(&DbConnectInf);
		if (!db)
		{
			wxMessageBox(wxT("Error: cannot get DB connection"));
			return -1;
		}
		else
		{
			//Qui faccio la query
			int numCol = db->GetColumnCount(wxT("runner"), wxT(""));
			return 1;
		}
	}
}

int ResegUP::GetUltimoTempo(SQLBIGINT *tempo)
{
	wxString sqlStmt;
	sqlStmt = wxString::Format(wxT("SELECT MAX(Timestamp) FROM runner where ID_Runner = %d"),ID_Runner);
	SDWORD cb;

	if (!db->ExecSql(sqlStmt.c_str()))
	{
		wxMessageBox(wxT("Errore Query"));
		*tempo = 0;
		return 0;
	}
	else
	{
		//Query eseguita
		while (db->GetNext())
		{
			if(!db->GetData(1,SQL_C_SBIGINT,tempo,sizeof(tempo),&cb))
			{
				wxMessageBox(wxT("Errore lettura dati"));
				return 0;
			}
			else
			{
				return 1;	
			}
		}
	}
}

int ResegUP::GetPutECGeACCfromDB(SampleStruct *Signal, SQLBIGINT *LastTime, SQLBIGINT Time)
{
	SDWORD cb;
	wxString sqlStmt;
	sqlStmt = wxString::Format(wxT("SELECT Timestamp,ECG,AccX,AccY,AccZ FROM runner where ID_Runner = %d && Timestamp>%.0f"),ID_Runner,(float)*LastTime);
	if (!db->ExecSql(sqlStmt.c_str()))
	{
		wxMessageBox(wxT("Errore Query"));
		return 0;
	}
	else
	{
		//Query eseguita, leggo uno alla volta i dati e li butto dentro
		while(db->GetNext())
		{
			if(!db->GetData(1,SQL_C_SBIGINT,&Timestamp,sizeof(Timestamp),&cb))
			{
				wxMessageBox(wxT("Errore lettura dati"));
				return 0;
			}
			else
			{
				if (Timestamp<=Time)
				{
					if(!db->GetData(2,SQL_C_LONG,&ECG,sizeof(ECG),&cb))
					{
						wxMessageBox(wxT("Errore lettura dati"));
						return 0;
					}
					if(!db->GetData(3,SQL_C_LONG,&AccX,sizeof(AccX),&cb))
					{
						wxMessageBox(wxT("Errore lettura dati"));
						return 0;
					}
					if(!db->GetData(4,SQL_C_LONG,&AccY,sizeof(AccY),&cb))
					{
						wxMessageBox(wxT("Errore lettura dati"));
						return 0;
					}
					if(!db->GetData(5,SQL_C_LONG,&AccZ,sizeof(AccZ),&cb))
					{
						wxMessageBox(wxT("Errore lettura dati"));
						return 0;
					}
					
					Signal->Data.Signal[0] = (int)ECG;
					Signal->Data.Signal[1] = (int)AccX;
					Signal->Data.Signal[2] = (int)AccY;
					Signal->Data.Signal[3] = (int)AccZ;
					PutSample(Signal);
				}
				else
					break;
			}
		}
		*LastTime = Time;
		return 1;
	}
}

int ResegUP::ReadID(int *ID_List)
{
	SDWORD cb;
	wxString sqlStmt;
	SQLINTEGER ID;
	int i=0;
	sqlStmt = wxT("SELECT DISTINCT ID_Runner FROM runner");
	if (!db->ExecSql(sqlStmt.c_str()))
	{
		wxMessageBox(wxT("Errore Query"));
		return 0;
	}
	else
	{
		//Query eseguita, leggo uno alla volta i dati e li butto dentro
		while(db->GetNext())
		{
			if(!db->GetData(1,SQL_C_LONG,&ID,sizeof(SQLINTEGER),&cb))
			{
				wxMessageBox(wxT("Errore lettura dati"));
				return 0;
			}
			else
			{
				ID_List[i] = ID;
				i++;
			}
		}
		return i;
	}
	
}